var menu = {

    data : {

    },
    start : function () {
        this.onLoad();
        this.bindEvent();
    },
    onLoad : function () {
        this.initTreeTable();
    },
    bindEvent : function () {
        var _this = this;
        $("#add-menu-btn").click(function () {
           _this.addMenu();
        });
        $("#add-sub-function-btn").click(function () {
            _this.openFunctionSelecter();
        });
        $("#update-function-btn").click(function () {
            _this.openFunctionSelecter();
        });
        $("#add-icon-btn").click(function () {
            _this.openIconSelecter();
        });
        $("#add-sub-icon-btn").click(function () {
            _this.openIconSelecter();
        });
        $("#updateFirstLevel-icon-btn").click(function () {
            _this.openIconSelecter();
        });
        $("#update-icon-btn").click(function () {
            _this.openIconSelecter();
        });
        $("#add-menuName").blur(function () {
            _this.validateMenuName("add-menuName");
        });
        $("#add-sub-menuName").blur(function () {
            _this.validateMenuName("add-sub-menuName");
        });
        $("#updateFirstLevel-menuName").blur(function () {
            _this.validateMenuName("updateFirstLevel-menuName", "updateFirstLevel-menuId");
        });
        $("#update-menuName").blur(function () {
            _this.validateMenuName("update-menuName", "update-menuId");
        });
    },
    initTreeTable : function () {
        $('#menuTable').bootstrapTreeTable({
            id: 'menuId',
            code: 'menuId',
            parentCode: 'parentId',
            type: "POST",
            url: sinoUtil.httpPrefix + '/api/menu/getAllMenus',
            ajaxParams: {Authorization: window.localStorage.token},
            expandColumn: '1',
            striped: true,
            bordered: true,
            expandAll: false,
            columns: [
                {
                    title: '编号',
                    field: 'menuId',
                    visible: false,
                    align: 'center',
                    valign: 'center',
                    width: '5%'
                },
                {
                    title: '名称',
                    valign: 'center',
                    field: 'menuName',
                    width: '20%'
                },
                {
                    title: '图标',
                    field: 'menuIcon',
                    align: 'center',
                    valign: 'center',
                    width : '5%',
                    formatter: function (item, index) {
                        return item.menuIcon == null ? '' : '<i class="' + item.menuIcon + ' fa-lg"></i>';
                    }
                },
                {
                    title: '地址',
                    valign: 'center',
                    width : '20%',
                    field: 'menuUrl'
                },
                {
                    title: '绑定功能',
                    valign: 'center',
                    width : '20%',
                    field: 'functionRight'
                },
                {
                    title: '操作',
                    field: 'id',
                    align: 'center',
                    valign: 'center',
                    formatter: function (item, index) {
                        var a = "<a class='layui-btn layui-btn-xs' href='#' mce_href='#' title='添加下级菜单' onclick='menu.addSubMenu(" + JSON.stringify(item) + ")'>添加下级</a> ";
                        var u = "<a class='layui-btn layui-btn-xs' href='#' mce_href='#' title='编辑菜单' onclick='menu.updateMenu(" + JSON.stringify(item) + ")'>编辑</a> ";
                        var d = "<a class='layui-btn layui-btn-danger layui-btn-xs' href='#' mce_href='#' title='删除菜单' onclick='menu.deleteMenu(" + JSON.stringify(item)  + ")'>删除</a> ";
                        return a + u + d;
                    }
                }]
        });
    },
    addMenu : function () {
        var _this = this;
        var index = layer.open({
            type: 1,
            title: "新增菜单",
            skin: "layui-layer-lan",
            area: ['800px', '520px'],
            content: $("#addMenuDialog"),
            btn: ['提交', '取消'],
            btnAlign: 'c',
            skin: 'demo-class',
            success: function (layero, index) {
                $("#add-parentMenuId").val("0");
                $("#add-parentMenuName").val("根节点");
            },
            end: function () {
                $("#add-parentMenuId").val("").removeClass("error").next("p").remove();
                $("#add-parentMenuName").val("").removeClass("error").next("p").remove();
                $("#add-menuName").val("").removeClass("error").next("p").remove();
                $("#add-menuSort").val("").removeClass("error").next("p").remove();
                $("#add-menuIcon").val("").removeClass("error").next("p").remove();
            },
            btn1: function(index, layero){
                var loadingIndex = sinoUtil.loading();
                var param = {
                    parentId       : $("#add-parentMenuId").val(),
                    parentMenuName : $("#add-parentMenuName").val(),
                    menuName       : $("#add-menuName").val(),
                    sortNum        : $("#add-menuSort").val(),
                    menuIcon       : $("#add-menuIcon").val()
                }
                if(!sinoUtil.validateAndChangeClass("#add-parentMenuId", sinoUtil.validate(param.parentId, 'require'), "父菜单id不能为空!")){
                    layer.close(loadingIndex);
                    return false;
                }
                if(!sinoUtil.validateAndChangeClass("#add-parentMenuName", sinoUtil.validate(param.parentMenuName, 'require'), "父菜单名称不能为空!")){
                    layer.close(loadingIndex);
                    return false;
                }
                if(!sinoUtil.validateAndChangeClass("#add-menuSort", sinoUtil.validate(param.sortNum, 'require'), "菜单排序不能为空!")){
                    layer.close(loadingIndex);
                    return false;
                }
                if(!sinoUtil.validateAndChangeClass("#add-menuSort", sinoUtil.validate(param.sortNum, 'number'), "菜单排序应为正整数!")){
                    layer.close(loadingIndex);
                    return false;
                }
                sinoUtil.sendHttp(sinoUtil.httpPrefix + "/api/menu/addMenu", "POST", param, function(res){
                    if(res.status == 0){
                        layer.close(index);
                        _this.initTreeTable();
                        sinoUtil.stopLoadingAndShowResult(loadingIndex, 1, "新增成功");
                    }else if(res.status == 10 || res.status == 11){
                        window.parent.location.href = "/system/login";
                    }else{
                        sinoUtil.stopLoadingAndShowResult(loadingIndex, 2, res.msg);
                    }
                }, function(error){
                    sinoUtil.stopLoadingAndShowResult(loadingIndex, 2, "发生未知错误，请刷新重试");
                }, false);
            }
        });
    },
    addSubMenu : function (currentMenuInfo) {
        var _this = this;
        var index = layer.open({
            type: 1,
            title: "新增下级菜单",
            skin: "layui-layer-lan",
            area: ['800px', '520px'],
            content: $("#addSubMenuDialog"),
            btn: ['提交', '取消'],
            btnAlign: 'c',
            skin: 'demo-class',
            success: function (layero, index) {
                $("#add-sub-parentMenuId").val(currentMenuInfo.menuId);
                $("#add-sub-parentMenuName").val(currentMenuInfo.menuName);
            },
            end: function () {
                $("#add-sub-parentMenuId").val("").removeClass("error").next("p").remove();
                $("#add-sub-parentMenuName").val("").removeClass("error").next("p").remove();
                $("#add-sub-menuName").val("").removeClass("error").next("p").remove();
                $("#add-sub-menuUrl").val("").removeClass("error").next("p").remove();
                $("#add-sub-menuSort").val("").removeClass("error").next("p").remove();
                $("#add-sub-menuFunction").val("").removeClass("error").next("p").remove();
                $("#add-sub-menuIcon").val("").removeClass("error").next("p").remove();
            },
            btn1: function(index, layero){
                var loadingIndex = sinoUtil.loading();
                var param = {
                    parentId              : $("#add-sub-parentMenuId").val(),
                    parentMenuName        : $("#add-sub-parentMenuName").val(),
                    menuName              : $("#add-sub-menuName").val(),
                    menuUrl               : $("#add-sub-menuUrl").val(),
                    sortNum               : $("#add-sub-menuSort").val(),
                    functionRightCode     : $("#add-sub-menuFunction").val(),
                    menuIcon              : $("#add-sub-menuIcon").val()
                }
                if(!sinoUtil.validateAndChangeClass("#add-sub-parentMenuId", sinoUtil.validate(param.parentId, 'require'), "父菜单id不能为空!")){
                    layer.close(loadingIndex);
                    return false;
                }
                if(!sinoUtil.validateAndChangeClass("#add-sub-parentMenuName", sinoUtil.validate(param.parentMenuName, 'require'), "父菜单名称不能为空!")){
                    layer.close(loadingIndex);
                    return false;
                }
                if(!sinoUtil.validateAndChangeClass("#add-sub-menuUrl", sinoUtil.validate(param.menuUrl, 'require'), "链接地址不能为空!")){
                    layer.close(loadingIndex);
                    return false;
                }
                if(!sinoUtil.validateAndChangeClass("#add-sub-menuSort", sinoUtil.validate(param.sortNum, 'require'), "菜单排序不能为空!")){
                    layer.close(loadingIndex);
                    return false;
                }
                if(!sinoUtil.validateAndChangeClass("#add-sub-menuSort", sinoUtil.validate(param.sortNum, 'number'), "菜单排序应为正整数!")){
                    layer.close(loadingIndex);
                    return false;
                }
                if(!sinoUtil.validateAndChangeClass("#add-sub-menuFunction", sinoUtil.validate(param.functionRightCode, 'require'), "绑定功能不能为空!")){
                    layer.close(loadingIndex);
                    return false;
                }
                sinoUtil.sendHttp(sinoUtil.httpPrefix + "/api/menu/addMenu", "POST", param, function(res){
                    if(res.status == 0){
                        layer.close(index);
                        _this.initTreeTable();
                        sinoUtil.stopLoadingAndShowResult(loadingIndex, 1, "新增成功");
                    }else if(res.status == 10 || res.status == 11){
                        window.parent.location.href = "/system/login";
                    }else{
                        sinoUtil.stopLoadingAndShowResult(loadingIndex, 2, res.msg);
                    }
                }, function(error){
                    sinoUtil.stopLoadingAndShowResult(loadingIndex, 1, "发生未知错误，请刷新重试");
                }, false);
            }
        });
    },
    updateMenu : function (currentMenuInfo) {
        var _this = this;
        if(currentMenuInfo.parentId == 0){
            var index = layer.open({
                type: 1,
                title: "编辑一级菜单",
                skin: "layui-layer-lan",
                area: ['800px', '520px'],
                content: $("#updateFirstLevelMenuDialog"),
                btn: ['提交', '取消'],
                btnAlign: 'c',
                skin: 'demo-class',
                success: function (layero, index) {
                    $("#updateFirstLevel-menuId").val(currentMenuInfo.menuId);
                    $("#updateFirstLevel-parentMenuId").val(currentMenuInfo.parentId);
                    $("#updateFirstLevel-parentMenuName").val(currentMenuInfo.parentMenuName);
                    $("#updateFirstLevel-menuName").val(currentMenuInfo.menuName);
                    $("#updateFirstLevel-menuSort").val(currentMenuInfo.sortNum);
                    $("#updateFirstLevel-menuIcon").val(currentMenuInfo.menuIcon);
                },
                end: function () {
                    $("#updateFirstLevel-menuId").val("").removeClass("error").next("p").remove();
                    $("#updateFirstLevel-parentMenuId").val("").removeClass("error").next("p").remove();
                    $("#updateFirstLevel-parentMenuName").val("").removeClass("error").next("p").remove();
                    $("#updateFirstLevel-menuName").val("").removeClass("error").next("p").remove();
                    $("#updateFirstLevel-menuSort").val("").removeClass("error").next("p").remove();
                    $("#updateFirstLevel-menuIcon").val("").removeClass("error").next("p").remove();
                },
                btn1: function(index, layero){
                    var loadingIndex = sinoUtil.loading();
                    var param = {
                        menuId            : $("#updateFirstLevel-menuId").val(),
                        parentId          : $("#updateFirstLevel-parentMenuId").val(),
                        parentMenuName    : $("#updateFirstLevel-parentMenuName").val(),
                        menuName          : $("#updateFirstLevel-menuName").val(),
                        menuUrl           : $("#updateFirstLevel-menuUrl").val(),
                        sortNum           : $("#updateFirstLevel-menuSort").val(),
                        functionRight     : $("#updateFirstLevel-menuFunction").val(),
                        menuIcon          : $("#updateFirstLevel-menuIcon").val()
                    }
                    if(!sinoUtil.validateAndChangeClass("#updateFirstLevel-menuId", sinoUtil.validate(param.menuId, 'require'), "菜单id不能为空!")){
                        layer.close(loadingIndex);
                        return false;
                    }
                    if(!sinoUtil.validateAndChangeClass("#updateFirstLevel-parentMenuId", sinoUtil.validate(param.parentId, 'require'), "父菜单id不能为空!")){
                        layer.close(loadingIndex);
                        return false;
                    }
                    if(!sinoUtil.validateAndChangeClass("#updateFirstLevel-parentMenuName", sinoUtil.validate(param.parentMenuName, 'require'), "父菜单名称不能为空!")){
                        layer.close(loadingIndex);
                        return false;
                    }
                    if(!sinoUtil.validateAndChangeClass("#updateFirstLevel-menuSort", sinoUtil.validate(param.sortNum, 'require'), "菜单排序不能为空!")){
                        layer.close(loadingIndex);
                        return false;
                    }
                    if(!sinoUtil.validateAndChangeClass("#updateFirstLevel-menuSort", sinoUtil.validate(param.sortNum, 'number'), "菜单排序应为正整数!")){
                        layer.close(loadingIndex);
                        return false;
                    }
                    sinoUtil.sendHttp(sinoUtil.httpPrefix + "/api/menu/updateMenu", "POST", param, function(res){
                        if(res.status == 0){
                            layer.close(index);
                            _this.initTreeTable();
                            sinoUtil.stopLoadingAndShowResult(loadingIndex, 1, "修改成功");
                        }else if(res.status == 10 || res.status == 11){
                            window.parent.location.href = "/system/login";
                        }else{
                            sinoUtil.stopLoadingAndShowResult(loadingIndex, 2, res.msg);
                        }
                    }, function(error){
                        sinoUtil.stopLoadingAndShowResult(loadingIndex, 2, "发生未知错误，请刷新重试");
                    }, false);
                }
            });
        }else{
            var index = layer.open({
                type: 1,
                title: "编辑菜单",
                skin: "layui-layer-lan",
                area: ['800px', '520px'],
                content: $("#updateMenuDialog"),
                btn: ['提交', '取消'],
                btnAlign: 'c',
                skin: 'demo-class',
                success: function (layero, index) {
                    $("#update-menuId").val(currentMenuInfo.menuId);
                    $("#update-parentMenuId").val(currentMenuInfo.parentId);
                    $("#update-parentMenuName").val(currentMenuInfo.parentMenuName);
                    $("#update-menuName").val(currentMenuInfo.menuName);
                    $("#update-menuUrl").val(currentMenuInfo.menuUrl);
                    $("#update-menuSort").val(currentMenuInfo.sortNum);
                    $("#update-menuFunction").val(currentMenuInfo.functionRightCode);
                    $("#update-menuIcon").val(currentMenuInfo.menuIcon);
                },
                end: function () {
                    $("#update-menuId").val("").removeClass("error").next("p").remove();
                    $("#update-parentMenuId").val("").removeClass("error").next("p").remove();
                    $("#update-parentMenuName").val("").removeClass("error").next("p").remove();
                    $("#update-menuName").val("").removeClass("error").next("p").remove();
                    $("#update-menuUrl").val("").removeClass("error").next("p").remove();
                    $("#update-menuSort").val("").removeClass("error").next("p").remove();
                    $("#update-menuFunction").val("").removeClass("error").next("p").remove();
                    $("#update-menuIcon").val("").removeClass("error").next("p").remove();
                },
                btn1: function(index, layero){
                    var loadingIndex = sinoUtil.loading();
                    var param = {
                        menuId            : $("#update-menuId").val(),
                        parentId          : $("#update-parentMenuId").val(),
                        parentMenuName    : $("#update-parentMenuName").val(),
                        menuName          : $("#update-menuName").val(),
                        menuUrl           : $("#update-menuUrl").val(),
                        sortNum           : $("#update-menuSort").val(),
                        functionRightCode : $("#update-menuFunction").val(),
                        menuIcon          : $("#update-menuIcon").val()
                    }
                    if(!sinoUtil.validateAndChangeClass("#update-menuId", sinoUtil.validate(param.menuId, 'require'), "菜单id不能为空!")){
                        layer.close(loadingIndex);
                        return false;
                    }
                    if(!sinoUtil.validateAndChangeClass("#update-parentMenuId", sinoUtil.validate(param.parentId, 'require'), "父菜单id不能为空!")){
                        layer.close(loadingIndex);
                        return false;
                    }
                    if(!sinoUtil.validateAndChangeClass("#update-parentMenuName", sinoUtil.validate(param.parentMenuName, 'require'), "父菜单名称不能为空!")){
                        layer.close(loadingIndex);
                        return false;
                    }
                    if(!sinoUtil.validateAndChangeClass("#update-menuUrl", sinoUtil.validate(param.menuUrl, 'require'), "链接地址不能为空!")){
                        layer.close(loadingIndex);
                        return false;
                    }
                    if(!sinoUtil.validateAndChangeClass("#update-menuSort", sinoUtil.validate(param.sortNum, 'require'), "菜单排序不能为空!")){
                        layer.close(loadingIndex);
                        return false;
                    }
                    if(!sinoUtil.validateAndChangeClass("#update-menuSort", sinoUtil.validate(param.sortNum, 'number'), "菜单排序应为正整数!")){
                        layer.close(loadingIndex);
                        return false;
                    }
                    if(!sinoUtil.validateAndChangeClass("#update-menuFunction", sinoUtil.validate(param.functionRightCode, 'require'), "绑定功能不能为空!")){
                        layer.close(loadingIndex);
                        return false;
                    }
                    sinoUtil.sendHttp(sinoUtil.httpPrefix + "/api/menu/updateMenu", "POST", param, function(res){
                        if(res.status == 0){
                            layer.close(index);
                            _this.initTreeTable();
                            sinoUtil.stopLoadingAndShowResult(loadingIndex, 1, "修改成功");
                        }else if(res.status == 10 || res.status == 11){
                            window.parent.location.href = "/system/login";
                        }else{
                            sinoUtil.stopLoadingAndShowResult(loadingIndex, 2, res.msg);
                        }
                    }, function(error){
                        sinoUtil.stopLoadingAndShowResult(loadingIndex, 1, "发生未知错误，请刷新重试");
                    }, false);
                }
            });
        }
    },
    deleteMenu : function (currentMenuInfo) {
        var _this = this;
        layer.confirm('数据删除后将不可恢复,确定要删除吗?', function(index){
            layer.close(index);
            var loadingIndex = sinoUtil.loading();
            sinoUtil.sendHttp(sinoUtil.httpPrefix + "/api/menu/deleteMenu", "POST", {menuId : currentMenuInfo.menuId}, function(res){
                if(res.status == 0){
                    _this.initTreeTable();
                    sinoUtil.stopLoadingAndShowResult(loadingIndex, 1, "删除成功");
                }else if(res.status == 10 || res.status == 11){
                    window.parent.location.href = "/system/login";
                }else{
                    sinoUtil.stopLoadingAndShowResult(loadingIndex, 2, res.msg);
                }
            }, function(error){
                sinoUtil.stopLoadingAndShowResult(loadingIndex, 2, "发生未知错误，请刷新重试");
            }, false);
        })
    },
    openFunctionSelecter : function () {
        layer.open({
            type: 2,
            title:'功能列表',
            content: '/system/menuSelectFunctions',
            area: ['480px', '90%'],
            success: function(layero, index){

            }
        });
    },
    openIconSelecter : function () {
        layer.open({
            type: 2,
            title:'图标列表',
            content: '/system/menuSelectIcons',
            area: ['480px', '90%'],
            success: function(layero, index){

            }
        });
    },
    validateMenuName : function (target, idTarget) {
        if(!sinoUtil.validateAndChangeClass("#" + target, sinoUtil.validate($("#" + target).val(), 'require'), "菜单名称不能为空!")){
            return false;
        }
        if(!sinoUtil.validateAndChangeClass("#" + target, sinoUtil.validate($("#" + target).val(), 'specialCharacter'), "菜单名称不能包含特殊字符!")){
            return false;
        }
        if(idTarget){
            sinoUtil.sendHttp(sinoUtil.httpPrefix + "/api/menu/validateMenuNameExSelf", "POST", {menuName : $("#" + target).val(), menuId : $("#" + idTarget).val()}, function(res){
                if(res.status == 0){
                    sinoUtil.validateAndChangeClass("#" + target, true);
                }else if(res.status == 10 || res.status == 11){
                    window.parent.location.href = "/system/login";
                }else{
                    sinoUtil.validateAndChangeClass("#" + target, false, "菜单名称不能重复");
                }
            }, function(error){
                layer.msg("发生未知错误，请刷新重试");
            }, false);
        }else{
            sinoUtil.sendHttp(sinoUtil.httpPrefix + "/api/menu/validateMenuName", "POST", {menuName : $("#" + target).val()}, function(res){
                if(res.status == 0){
                    sinoUtil.validateAndChangeClass("#" + target, true);
                }else if(res.status == 10 || res.status == 11){
                    window.parent.location.href = "/system/login";
                }else{
                    sinoUtil.validateAndChangeClass("#" + target, false, "菜单名称不能重复");
                }
            }, function(error){
                layer.msg("发生未知错误，请刷新重试");
            }, false);
        }
    }

}

$(function () {
    menu.start();
})